package com.project.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.filter.CharacterEncodingFilter;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@Controller
@RequestMapping("user")
public class UserController {
    @RequestMapping("login")
    public String login(String userName, String pwd, HttpServletRequest request){
        System.out.println(userName+"   "+pwd);
        if("java".equalsIgnoreCase(userName)&&
             "123".equalsIgnoreCase(pwd)){
            //在session中记录登陆信息
            request.getSession().setAttribute("loginUser", userName);
            return "/html/index.html";
        }

        return "/html/error.html";
    }

    @RequestMapping("add")
    @ResponseBody
    public String getInfo(String userName){
        System.out.println(userName);
        return userName;
    }
//
//    @RequestMapping("reg")
//    @ResponseBody
//    public String reg(@Valid SchoolBean school, BindingResult result) throws JsonProcessingException {
//        if(result.hasErrors()){
//            System.out.println(result.getFieldErrors());
//            return new ObjectMapper().writeValueAsString(result.getFieldErrors());
//        }
//
//        return "ok";
//    }
}
